package cn.school.hotel.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.school.hotel.dao.TripLabelDicDao;
import cn.school.hotel.entity.TripLabelDic;
import cn.school.hotel.service.TripLabelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service("tripLabelService")
public class TripLabelServiceImpl extends ServiceImpl<TripLabelDicDao, TripLabelDic> implements TripLabelService {

    @Autowired
    private TripLabelDicDao tripLabelDicDao;

    @Override
    public IPage<TripLabelDic> getLabelList(TripLabelDic vo, Integer pageSize, Integer pageNum) {
        LambdaQueryWrapper<TripLabelDic> wrapper = new LambdaQueryWrapper<>(vo);
        if (StrUtil.isNotEmpty(vo.getName())) {
            wrapper.eq(TripLabelDic::getName, vo.getName());
        }
        if (StrUtil.isNotEmpty(vo.getParentId())) {
            wrapper.eq(TripLabelDic::getParentId, vo.getParentId());
        }
        wrapper.orderByDesc(TripLabelDic::getCreationDate);

        return  page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public List<TripLabelDic> getLabelListByParentId(TripLabelDic vo) {
        LambdaQueryWrapper<TripLabelDic> wrapper = new LambdaQueryWrapper<>(vo);
        if (StrUtil.isNotEmpty(vo.getParentId())) {
            wrapper.eq(TripLabelDic::getParentId, vo.getParentId());
        }
        wrapper.orderByDesc(TripLabelDic::getCreationDate);
        return tripLabelDicDao.selectList(wrapper);
    }

    @Override
    public int createOrUpdateLabelDic(TripLabelDic vo) {
        int num = 0;
        if(StringUtils.isEmpty(vo.getId())){
            num = tripLabelDicDao.insert(vo);
        }else{
            num = tripLabelDicDao.updateById(vo);
        }
        return num;
    }

    @Override
    public int deleteLabelDicById(String id) {
        return tripLabelDicDao.deleteById(id);
    }
}
